package com.firefly.mobile.app.context.init;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.util.Log;

import com.firefly.mobile.app.config.Config;
import com.firefly.mobile.app.context.SytApplication;
import com.firefly.mobile.app.util.TransjnlUtil;

/**
 * config初始化工厂
 * 
 * @author ydw
 * 
 */
public class ConfigInitFactory {

	
	public void init(Context context) {
		try {
			String confFolder = "conf";
			
			String envFileName = "env.properties";
			//从asserts文件夹下load文件
			Properties prop = new Properties();
			prop.load(context.getAssets().open(confFolder+File.separator+envFileName));
			SytApplication.env = (String)prop.get("env");
			parseConfigs(context.getAssets().open(SytApplication.CONFIG_FILE_PATH),SytApplication.env);
			
			
			String fileName = "trans.properties";
			String filePath = SytApplication.getSdcardFilePath()+ File.separator+confFolder+File.separator+fileName;
			File file = new File(filePath);
			TransjnlUtil.TRANS_FILE_PATH = file.getAbsolutePath();
			String tmp = TransjnlUtil.genTraceNo();
			
			
//			TermInfoUploadTask.init();
//			MallInitTask.init();
			Log.i("ConfigInitFactory", "初始化流水号:"+tmp);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	private static void parseConfigs(InputStream is,String env){
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 取得DocumentBuilderFactory实例
			factory.setIgnoringElementContentWhitespace(true);
			factory.setIgnoringComments(true);
			DocumentBuilder builder = factory.newDocumentBuilder(); // 从factory获取DocumentBuilder实例
			Document doc = builder.parse(is); // 解析输入流 得到Document实例
			Element rootEle = doc.getDocumentElement();
			NodeList items = rootEle.getElementsByTagName("prd");
			for(int i=0;i<items.getLength();i++){
				Element bizpackItem=(Element)items.item(i);
				NodeList propItems = bizpackItem.getElementsByTagName("property");
				
				for(int j=0;j<propItems.getLength();j++){
					Element item=(Element)propItems.item(j);
					String key = item.getAttribute("name");
					String value = item.getAttribute("value");
					Config.CONF_MAP.put(key, value);
				}
			}
			//如果不是生产环境则覆盖生产环境配置
			if (!"prd".equals(env)) {
				NodeList devItems = rootEle.getElementsByTagName("dev");
				for(int i=0;i<devItems.getLength();i++){
					Element bizpackItem=(Element)devItems.item(i);
					NodeList propdevItems = bizpackItem.getElementsByTagName("property");
					for(int j=0;j<propdevItems.getLength();j++){
						Element item=(Element)propdevItems.item(j);
						String key = item.getAttribute("name");
						String value = item.getAttribute("value");
						Config.CONF_MAP.put(key, value);
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
